package com.zx.sa.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.ChatClient.ChatClientRequestSpec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@CrossOrigin(origins = "*")
@RequestMapping("/chat")
public class ChatController {

    private final ChatClient chatClient;

    @Autowired
    public ChatController(ChatClient.Builder builder) {
        this.chatClient = builder.build();
    }

    @GetMapping("/index")
    public String index(Model model) {
        return "chat";
    }

    /**
     * 文本
     */
    @RequestMapping("/chat")
    @ResponseBody
    public String chat(@RequestParam(value = "msg", defaultValue = "") String msg) {
        //使用prompt创建一个新的聊天窗口
        ChatClientRequestSpec prompt = this.chatClient.prompt();
        if (msg.equals("")) {
            msg = "你好";
        }

        // user 设置用户输入的内容
        // call 发起请求，获取结果
        // content 获取结果内容
        return prompt.user(msg).call().content();
    }

} 